package com.wohuijituan.whcsmerchant.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

import com.wohuijituan.whcsmerchant.R;
import com.wohuijituan.whcsmerchant.adapter.SelectBankCardAdapter;
import com.wohuijituan.whcsmerchant.bean.BankListBean;
import com.wohuijituan.whcsmerchant.callback.BankListCallBack;
import com.wohuijituan.whcsmerchant.url.ParamsKey;
import com.wohuijituan.whcsmerchant.url.Urls;
import com.wohuijituan.whcsmerchant.utils.LogUtils;
import com.wohuijituan.whcsmerchant.utils.MyOkHttpUtils;
import com.wohuijituan.whcsmerchant.utils.MyRequestParams;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;

/**
 * 说明：
 * 作者：朱世元
 * 时间： 2016/10/12 10:38
 * 版本：V1.0
 * 修改历史：
 */
public class CardManageActivity extends MyBaseActivity implements View.OnClickListener{
    private static final String TAG = "CardManageActivity";
    private static final int CARD_ADD_REQUEST = 45;
    private ListView lvCardList;
    private ImageButton titleGoBack;
    private TextView titleName;
    private TextView titleAction;

    private void assignViews() {
        lvCardList = (ListView) findViewById(R.id.lv_card_list);
        titleGoBack = (ImageButton) findViewById(R.id.title_go_back);
        titleName = (TextView) findViewById(R.id.title_name);
        titleAction = (TextView) findViewById(R.id.title_tv_action);
    }
    private static final int BANK_LIST_REQUEST_OK = 4449;
    private static final int BANK_LIST_REQUEST_FAIL = 4448;
    private MyOkHttpUtils myOkHttpUtils;
    private MyRequestParams params;
    private SelectBankCardAdapter bankCardAdapter;
    private List<BankListBean.DataEntity> bankList;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_card_manage);
        assignViews();
        initData();
        initView();
        initListener();
        bankListRequest();
    }
    private void initData() {
        myOkHttpUtils = new MyOkHttpUtils();
        params = new MyRequestParams();
        bankList = new ArrayList<>();
        bankCardAdapter = new SelectBankCardAdapter(this, bankList);
        lvCardList.setAdapter(bankCardAdapter);
    }
    private void initView() {
        titleName.setText("我的银行卡");
        titleAction.setText("添加");
        titleGoBack.setVisibility(View.VISIBLE);
        titleAction.setVisibility(View.VISIBLE);
    }
    private void initListener() {
        titleAction.setOnClickListener(this);
        titleAction.setOnClickListener(this);
        lvCardList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                LogUtils.e(TAG,"onItemClick:"+position);
                bankCardAdapter.setCheckedPosition(position);
            }
        });
    }
    /**
     * 网络请求,获取银行卡列表
     */
    private void bankListRequest() {
        showDialog(ctx);
        String strUrl = Urls.BankList;
        params.clear();
        params.addStringRequest(ParamsKey.UserKey, myApplication.getUserKey());
        myOkHttpUtils.postRequest(strUrl, params, new BankListCallBack() {
            @Override
            public void onError(Call call, Exception e, int id) {
                showToast(ctx, "网络出错,请检查网络是否连接!");
                LogUtils.e(TAG, e.getMessage());
                handler.sendEmptyMessage(BANK_LIST_REQUEST_FAIL);
            }
            @Override
            public void onResponse(BankListBean response, int id) {

                if (response.getState() == 1) {//如果银行卡列表不为零个则成功,否则失败
                    if (response.getData().size() > 0) {
                        Message msg = new Message();
                        msg.what = BANK_LIST_REQUEST_OK;
                        msg.obj = response;
                        handler.sendMessage(msg);
                    } else {
                        handler.sendEmptyMessage(BANK_LIST_REQUEST_FAIL);
                    }

                } else {
                    showToast(ctx, response.getMsg());
                    handler.sendEmptyMessage(BANK_LIST_REQUEST_FAIL);
                }
            }
        });
    }
    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case BANK_LIST_REQUEST_OK:
                    // 银行卡列表请求成功
                    BankListBean bankListBean = (BankListBean) msg.obj;
                    bankCardAdapter.replace(bankListBean.getData());
                    dismissDialog();
                    break;
                case BANK_LIST_REQUEST_FAIL:
                    // TODO: 2016/10/8 改变页面为没有银行卡状态
                    dismissDialog();
                    break;
            }
        }
    };


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.title_tv_action:
                //跳转到添加银行卡界面
                Intent intent = new Intent(this,CardAddActivity.class);
                startActivityForResult(intent,CARD_ADD_REQUEST);
                break;
            case R.id.title_go_back:
                onBackPressed();
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode==CARD_ADD_REQUEST&&resultCode==RESULT_OK){
            bankListRequest();
        }
    }
}
